/*
 * Copyright 2011 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.ritesdk;

import java.text.NumberFormat;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

abstract public class AbstractMainClass {

  protected NumberFormat nf = NumberFormat.getInstance();
  {
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(3);
  }
  
  protected void showHelp(Options options) {
    HelpFormatter h = new HelpFormatter();
    h.printHelp("help", options);
  }
  
  public void run(String[] args) {
    Options options = createOptions();
    try {
      CommandLineParser parser = new PosixParser();
      CommandLine cmd = parser.parse(options, args);
      if (cmd.hasOption("h")) {
        showHelp(options);
        System.exit(-1);
      }
      Task task = null;
      try {
        task = Task.valueOf(cmd.getOptionValue("t").toUpperCase()); // e.g. "BC"
      } catch (Exception e) {
        System.err.println("ERROR: Please specify the valid task name with -t option.");
        System.exit(-1);
      }
      run(cmd, task);
    } catch (Exception e) {
      e.printStackTrace();
      showHelp(options);
      System.exit(-1);
    }
  }
  
  abstract protected void run(CommandLine cmd, Task task) throws Exception;
 
  abstract protected Options createOptions();
  
}
